package jmu.cultural_tourism.service.impl;

import jmu.cultural_tourism.dao.PostImageDao;
import jmu.cultural_tourism.dao.PostTagDao;
import jmu.cultural_tourism.dao.PostTagInfoDao;
import jmu.cultural_tourism.entity.PostImage;
import jmu.cultural_tourism.entity.PostInfo;
import jmu.cultural_tourism.dao.PostInfoDao;
import jmu.cultural_tourism.entity.PostTag;
import jmu.cultural_tourism.entity.PostTagInfo;
import jmu.cultural_tourism.service.PostInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import jakarta.annotation.Resource;

import java.util.List;

/**
 * 帖子表(PostInfo)表服务实现类
 *
 * @author makejava
 * @since 2025-07-17 10:38:43
 */
@Service("postInfoService")
public class PostInfoServiceImpl implements PostInfoService {
    @Autowired
    private PostInfoDao postInfoDao;
    @Autowired
    private PostImageDao postImageDao;
    @Autowired
    private PostTagInfoDao postTagInfoDao;

    /*通过ID查询单条数据*/
    @Override
    public PostInfo queryById(Integer postId) {
        return this.postInfoDao.queryById(postId);
    }

    @Override
    public List<PostInfo> queryAll(PageRequest pageRequest) {

        return postInfoDao.queryAll(pageRequest);
    }

    @Override
    public Page<PostInfo> fuzzyPost(String fuzzyContent, PageRequest pageRequest) {
        long total = this.postInfoDao.countAll();
        return new PageImpl<>(postInfoDao.fuzzyPost(fuzzyContent, pageRequest), pageRequest, total);
//        return postInfoDao.fuzzyPost(fuzzyContent, pageRequest);
    }

    @Override
    public Page<PostInfo> queryAllByLikeDesc(PageRequest pageRequest) {
        long total = this.postInfoDao.countAll();
        return new PageImpl<>(postInfoDao.queryAllByLikeDesc(pageRequest), pageRequest, total);
//        return postInfoDao.queryAllByLikeDesc(pageRequest);
    }


    /*分页查询*/
    @Override
    public Page<PostInfo> queryByPage(PostInfo postInfo, PageRequest pageRequest) {
        long total = this.postInfoDao.count(postInfo);
        return new PageImpl<>(postInfoDao.queryAllByLimit(postInfo, pageRequest), pageRequest, total);
    }

    /*新增数据*/
    @Override
    public PostInfo insert(PostInfo postInfo) {
        postInfoDao.insert(postInfo);

        PostImage postImage = new PostImage();
        postImage.setPostId(postInfo.getPostId());
        for (int i = 0; i < postInfo.getImageUrls().size(); i++) {
            postImage.setImageUrl(postInfo.getImageUrls().get(i));
            postImageDao.insert(postImage);
        }
        PostTagInfo postTagInfo = new PostTagInfo();
        postTagInfo.setPostId(postInfo.getPostId());
        for (int i = 0; i < postInfo.getTagIds().size(); i++) {
            postTagInfo.setPostTagId(postInfo.getTagIds().get(i));
            postTagInfoDao.insert(postTagInfo);
        }
        return postInfo;
    }

    /*修改数据*/
    @Override
    public PostInfo update(PostInfo postInfo) {
        this.postInfoDao.update(postInfo);
        return this.queryById(postInfo.getPostId());
    }

    /**
     * 通过主键删除数据
     *
     * @param postId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer postId) {
        return this.postInfoDao.deleteById(postId) > 0;
    }


    /**
     * 通过userId找到用户收藏的帖子
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public Page<PostInfo> queryCollectedPostsByUserId(Integer userId, PageRequest pageRequest) {
        long total = this.postInfoDao.countCollectedPostsByUserId(userId);
        return new PageImpl<>(postInfoDao.queryCollectedPostsByUserId(userId, pageRequest), pageRequest, total);
    }

    // 是否收藏帖子
    @Override
    public boolean isPostCollected(Integer userId, Integer postId) {
        // 当查询结果大于0时，表示用户已收藏该帖子
        return postInfoDao.checkPostCollected(userId, postId) > 0;
    }

    //新增收藏
    @Override
    public boolean addPostCollection(Integer userId, Integer postId) {
        // 插入收藏记录，返回影响行数（1为成功，0为失败）
        return postInfoDao.addPostCollection(userId, postId) > 0;
    }

    @Override
    public boolean deletePostCollection(Integer userId, Integer postId) {
        // 删除收藏记录，返回影响行数（1为成功，0为失败）
        return postInfoDao.deletePostCollection(userId, postId) > 0;
    }
}
